package com.android.smsmessing.adapter;

import java.util.ArrayList;
import java.util.Date;

import com.android.smsmessing.R;
import com.android.smsmessing.adapter.PeoplesAdapter.ViewHolder;
import com.android.smsmessing.model.PeopleVO;
import com.android.smsmessing.model.SMSVO;
import com.android.smsmessing.utils.DateUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class SMSAdapter extends BaseAdapter {

	private Context context;
	private LinearLayout peopleLineLayout;
	private EditText headTxtSearch;
	Date displayDate = new Date();
	Date now = new Date();
	public SMSAdapter(Context c) {
		this.context = c;
		mInflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private ArrayList<SMSVO> smses = null;
	private LayoutInflater mInflater = null;

	@Override
	public int getCount() {
		if (smses != null) {
			return smses.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		TextView txtSmsName;
		TextView txtSmsDetail;
		TextView txtSmsTime;
		CheckBox chkSmsSelect;
		public SMSVO sms;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.list_sms, null);
			holder.txtSmsName = (TextView) convertView
					.findViewById(R.id.txtSmsName);
			holder.txtSmsDetail = (TextView) convertView
					.findViewById(R.id.txtSmsDetail);
			holder.txtSmsTime = (TextView) convertView
					.findViewById(R.id.txtSmsTime);
			holder.chkSmsSelect = (CheckBox)convertView.findViewById(R.id.chkSmsSelect);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SMSVO currentSMS = smses.get(position);
		
		if(currentSMS.type == 1){
			holder.txtSmsName.setText(currentSMS.name + ": ");
		}else{
			holder.txtSmsName.setText("我: ");
		}
		holder.txtSmsName.getPaint().setFakeBoldText(true);
		
		
		
		holder.txtSmsDetail.setText(currentSMS.detail);
		holder.txtSmsDetail.getPaint().setFakeBoldText(true);
		
		displayDate.setTime(currentSMS.time);
		holder.txtSmsTime.setText(DateUtils.displayTime(now,displayDate));
		
		holder.sms = currentSMS;
		convertView.setTag(holder);
		
		return convertView;
	}

	public ArrayList<SMSVO> getSmses() {
		return smses;
	}

	public void setSmses(ArrayList<SMSVO> smses) {
		this.smses = smses;
		notifyDataSetChanged();
	}

	public void addSMS(SMSVO sms) {
		if (smses == null) {
			smses = new ArrayList();
		}
		smses.add(sms);
	}
}
